'use strict';

const Controller = require('egg').Controller;
/**
 * @Controller Job
 */
class JobController extends Controller {
    async index() {
        const ctx = this.ctx;
        try {
            const jobList = await ctx.service.job.find(ctx.query);
            ctx.body = {
                code: 200,
                data: jobList,
                success: true,
                msg: ``
            };
        }
        catch (err) {
            throw err;
        }
    }

    async create() {
        const ctx = this.ctx;

        try {
            const newJob = await ctx.service.job.create(ctx.request.body);
            ctx.body = {
                code: 200,
                data: newJob,
                success: true,
                msg: ``
            };
            console.log(newJob);
        }
        catch (err) {
            throw err;
        }

    };
    async update() {
        const ctx = this.ctx;
        // ctx.validate(deleteRule, ctx.params);
        const result = await this.ctx.service.job.updateOneJob({_id: ctx.params.id}, ctx.request.body);
        if(result.ok) {
            ctx.body = {
                code: 200,
                data: {},
                success: true,
                msg: `修改成功`
            }
        }
    }
    async destroy() {
        const ctx = this.ctx;
        // ctx.validate(deleteRule, ctx.params);
        const result = await this.ctx.service.job.deleteJob(ctx.params.id);
        if(result.ok) {
            ctx.body = {
                code: 200,
                data: {},
                success: true,
                msg: `删除成功`
            }
        }
    }
}

module.exports = JobController;
